package com.hskn.hss.module.violationHandleLog.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.module.carviolation.entity.CarViolation;
import com.hskn.hss.module.carviolation.mapper.CarViolationMapper;
import com.hskn.hss.module.dlbjviolation.entity.DlbjViolation;
import com.hskn.hss.module.dlbjviolation.mapper.DlbjViolationMapper;
import com.hskn.hss.module.messageFlowRecords.domain.MessageFlowRecords;
import com.hskn.hss.module.messageFlowRecords.service.IMessageFlowRecordsService;
import com.hskn.hss.module.tlcar.entity.TlCar;
import com.hskn.hss.module.tlcar.service.ITlCarService;
import com.hskn.hss.module.tlemployee.entity.TlEmployee;
import com.hskn.hss.module.tlemployee.service.ITlEmployeeService;
import com.hskn.hss.module.tlfacilities.entity.TlFacilities;
import com.hskn.hss.module.tlfacilities.service.ITlFacilitiesService;
import com.hskn.hss.module.user.entity.User;
import com.hskn.hss.module.user.mapper.UserMapper;
import com.hskn.hss.module.violationHandleLog.domain.ViolationHandleLog;
import com.hskn.hss.module.violationHandleLog.mapper.ViolationHandleLogMapper;
import com.hskn.hss.module.violationHandleLog.service.IViolationHandleLogService;
import com.hskn.hss.module.violationHandleLog.to.ViolationHandleLogListTO;
import com.hskn.hss.module.violationHandleLog.vo.ViolationHandleLogVO;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import com.hskn.hss.utils.UpdateCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 违规处理记录Service业务层处理
 * 
 * @author ccm
 * @date 2021-09-07
 */
@Service
public class ViolationHandleLogServiceImpl  extends ServiceImpl<ViolationHandleLogMapper,ViolationHandleLog> implements IViolationHandleLogService
{
    @Resource
    private ViolationHandleLogMapper violationHandleLogMapper;
    @Autowired
    private ITlEmployeeService employeeService;
    @Autowired
    private ITlCarService carService;
    @Autowired
    private ITlFacilitiesService facilitiesService;
    @Autowired
    private IMessageFlowRecordsService messageFlowRecordsService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CarViolationMapper carViolationMapper;
    @Resource
    private DlbjViolationMapper dlbjViolationMapper;

    /**
     * 查询违规处理记录
     * 
     * @param id 违规处理记录主键
     * @return 违规处理记录
     */
    @Override
    public ViolationHandleLog selectViolationHandleLogById(String id)
    {
        return violationHandleLogMapper.selectViolationHandleLogById(id);
    }

    /**
     * 查询违规处理记录列表
     * 
     * @param violationHandleLog 违规处理记录
     * @return 违规处理记录
     */
    @Override
    public List<ViolationHandleLog> selectViolationHandleLogList(ViolationHandleLog violationHandleLog)
    {
        return violationHandleLogMapper.selectViolationHandleLogList(violationHandleLog);
    }
    /**
     * 查询报警处理记录列表
     *
     * @param alarmHandleLogListTO 报警处理记录
     * @return 报警处理记录
     */
    @Override
    public List<ViolationHandleLogVO> selectViolationHandleLogListByViolationId(ViolationHandleLogListTO alarmHandleLogListTO)
    {
        String uid = LoginUtils.getUser().getUid();
        QueryWrapper<ViolationHandleLog> alarmHandleLogQueryWrapper=new QueryWrapper<>();
        alarmHandleLogQueryWrapper.lambda().eq(ViolationHandleLog::getViolationId,alarmHandleLogListTO.getViolationId());
        alarmHandleLogQueryWrapper.lambda().orderByDesc(ViolationHandleLog::getCreateTime);
        List<ViolationHandleLog> alarmHandleLogList=violationHandleLogMapper.selectList(alarmHandleLogQueryWrapper);
        List<ViolationHandleLogVO> alarmHandleLogVOList= JSONObject.parseArray(JSONObject.toJSONString(alarmHandleLogList),ViolationHandleLogVO.class);
        Map<String, TlEmployee> employeeMap=employeeService.getEmpMap(new QueryWrapper<>());
        Map<String, TlCar> carMap=carService.getCarMap(new QueryWrapper<>());

        DlbjViolation dlbjViolation = dlbjViolationMapper.selectById(alarmHandleLogListTO.getViolationId());
        if (Objects.nonNull(dlbjViolation)){
            MessageFlowRecords messageFlowRecords = new MessageFlowRecords();
            messageFlowRecords.setMessageId(dlbjViolation.getDlbjViolationId());
            messageFlowRecords.setKind(2);
            messageFlowRecords.setSubjectType(1);
            messageFlowRecords.setUserId(uid);
            List<MessageFlowRecords> messageFlowRecords1 = messageFlowRecordsService.selectMessageFlowRecordsList(messageFlowRecords);
            if (Objects.isNull(messageFlowRecords1) || messageFlowRecords1.size() == 0){
                messageFlowRecords.setCreateTime(new Date());
                messageFlowRecords.setId(StringUtils.uuid());
                messageFlowRecords.setSubjuctId(dlbjViolation.getPersonId());
                messageFlowRecords.setSubjuctName(dlbjViolation.getPersonName());
                messageFlowRecords.setContent(dlbjViolation.getContent());
                User user=userMapper.selectById(uid);
                if (user!=null) {
                    messageFlowRecords.setUserName(user.getName());
                }
                messageFlowRecordsService.insertMessageFlowRecordsOnly(messageFlowRecords);
            }
        }

        CarViolation carViolation = carViolationMapper.selectById(alarmHandleLogListTO.getViolationId());
        if (Objects.nonNull(carViolation)){
            MessageFlowRecords messageFlowRecords = new MessageFlowRecords();
            messageFlowRecords.setMessageId(carViolation.getCarViolationId());
            messageFlowRecords.setKind(2);
            messageFlowRecords.setSubjectType(2);
            messageFlowRecords.setUserId(uid);
            List<MessageFlowRecords> messageFlowRecords1 = messageFlowRecordsService.selectMessageFlowRecordsList(messageFlowRecords);
            if (Objects.isNull(messageFlowRecords1) || messageFlowRecords1.size() == 0){
                messageFlowRecords.setCreateTime(new Date());
                messageFlowRecords.setId(StringUtils.uuid());
                messageFlowRecords.setSubjuctId(carViolation.getCarId());
                messageFlowRecords.setSubjuctName(carViolation.getCarName());
                messageFlowRecords.setContent(carViolation.getContent());
                User user=userMapper.selectById(uid);
                if (user!=null) {
                    messageFlowRecords.setUserName(user.getName());
                }
                messageFlowRecordsService.insertMessageFlowRecordsOnly(messageFlowRecords);
            }
        }

        for(ViolationHandleLogVO alarmHandleLogVO:alarmHandleLogVOList){
            if (alarmHandleLogVO.getAlarmSign().equals("emp")){
                if(employeeMap.containsKey(alarmHandleLogVO.getSubjectId())){
                    alarmHandleLogVO.setSubjectHeadPortrait(employeeMap.get(alarmHandleLogVO.getSubjectId()).getPersonphoto());
                }
            }else if (alarmHandleLogVO.getAlarmSign().equals("car")){
                if(carMap.containsKey(alarmHandleLogVO.getSubjectId())){
                    alarmHandleLogVO.setSubjectHeadPortrait(carMap.get(alarmHandleLogVO.getSubjectId()).getCarpicture());
                }
            }
            if (alarmHandleLogVO.getViolationTime()!=null) {
                alarmHandleLogVO.setViolationTimeString(Tools.getDateString(alarmHandleLogVO.getViolationTime()));
            }
        }
        return alarmHandleLogVOList;
    }

    /**
     * 新增违规处理记录
     * 
     * @param violationHandleLog 违规处理记录
     * @return 结果
     */
    @Override
    public int insertViolationHandleLog(ViolationHandleLog violationHandleLog)
    {
        violationHandleLog.setCreateTime(new Date());
        return violationHandleLogMapper.insertViolationHandleLog(violationHandleLog);
    }

    /**
     * 修改违规处理记录
     * 
     * @param violationHandleLog 违规处理记录
     * @return 结果
     */
    @Override
    public int updateViolationHandleLog(ViolationHandleLog violationHandleLog)
    {
        return violationHandleLogMapper.updateViolationHandleLog(violationHandleLog);
    }

    /**
     * 批量删除违规处理记录
     * 
     * @param ids 需要删除的违规处理记录主键
     * @return 结果
     */
    @Override
    public int deleteViolationHandleLogByIds(String[] ids)
    {
        return violationHandleLogMapper.deleteViolationHandleLogByIds(ids);
    }

    /**
     * 删除违规处理记录信息
     * 
     * @param id 违规处理记录主键
     * @return 结果
     */
    @Override
    public int deleteViolationHandleLogById(String id)
    {
        return violationHandleLogMapper.deleteViolationHandleLogById(id);
    }
}
